﻿
Public Class Convert
    Public Shared Function Dec2BinStr(ByVal Dec As Long) As String
        Dim x As String, D As Long
        x = ""
        D = Dec
        Do While D > 0
            x = (D Mod 2).ToString + x
            D = Math.Floor(D / 2)
        Loop
        If Dec = 0 Then x = "0"
        Return x
    End Function
    Public Shared Function Dec2BinAry(ByVal Dec As Long) As Integer()
        Dim x() As Integer, D As Long, i As Integer = 0
        Dec = Dec
        Do While D > 0
            x(i) = D Mod 2
            D = CInt(D / 2)
            i = i + 1
        Loop
        Return x
    End Function
    Public Shared Function OctStr2Dec(ByVal Oct As String) As Long
        Dim D As Long
        D = 0
        For i = 1 To Oct.Length
            D = D + CInt(Oct.Chars(i - 1).ToString) * 8 ^ (i - 1)
        Next
        Return D
    End Function
    Public Shared Function BinStr2Dec(ByVal Bin As String) As Long
        Dim D As Long
        D = 0
        For i = 1 To Bin.Length
            D = D + CInt(Bin.Substring(Bin.Length - i, 1)) * 2 ^ (i - 1)
        Next
        Return D
    End Function
    Public Shared Function BinAry2Dec(ByVal Bin() As Integer) As Long
        Dim D As Long
        D = 0
        For i = 0 To Bin.Count - 1
            D = D + Bin(i) * 2 ^ i
        Next
        Return D
    End Function
    Public Shared Function HexStr2Dec(ByVal Hex As String) As Long
        Dim D As Long, c As String, H As Integer
        D = 0
        For i = 1 To Hex.Length
            H = 0
            c = Hex.Substring(Hex.Length - i, 1)
            If c = "A" Or c = "a" Then H = 10
            If c = "B" Or c = "b" Then H = 11
            If c = "C" Or c = "c" Then H = 12
            If c = "D" Or c = "d" Then H = 13
            If c = "E" Or c = "e" Then H = 14
            If c = "F" Or c = "f" Then H = 15
            If H = 0 Then H = CInt(c)
            D = D + H * 16 ^ (i - 1)
        Next
        Return D
    End Function
    Public Shared Function Dec2HexStr(ByVal Dec As Long) As String
        Dim Hex As String = ""
        Dim D As Long, h As Long
        D = Dec
        Do While D > 0
            h = D Mod 16
            If h > 9 Then
                If h = 10 Then Hex = "A" + Hex
                If h = 11 Then Hex = "B" + Hex
                If h = 12 Then Hex = "C" + Hex
                If h = 13 Then Hex = "D" + Hex
                If h = 14 Then Hex = "E" + Hex
                If h = 15 Then Hex = "F" + Hex
            End If
            If h < 10 Then Hex = h.ToString + Hex
            D = Math.Floor(D / 16)
        Loop
        If Dec = 0 Then Hex = "0"
        Return Hex

    End Function
    Public Shared Function Dec2OctStr(ByVal Dec As Long) As String
        Dim x As String, D As Long
        x = ""
        D = Dec
        Do While D > 0
            x = (D Mod 8).ToString + x
            D = Math.Floor(D / 8)
        Loop
        If Dec = 0 Then x = "0"
        Return x
    End Function
    Public Shared Function HexStr2BinStr(ByVal Hex As String) As String
        Dim x As Long, b As String
        x = HexStr2Dec(Hex)
        b = Dec2BinStr(x)
        Return b
    End Function
    Public Function BinStr2HexStr(ByVal Bin As String) As String
        Dim b As Long, H As String
        b = BinStr2Dec(Bin)
        H = Dec2HexStr(b)
        Return H
    End Function
    Public Function BinStrAnd(ByVal Bin1 As String, ByVal Bin2 As String) As String
        Dim b1 As Long, b2 As Long, r As Long, s As String
        b1 = BinStr2Dec(Bin1)
        b2 = BinStr2Dec(Bin2)
        r = b1 And b2
        s = Dec2BinStr(r)
        Return s
    End Function
    Public Function HexStrAnd(ByVal Hex1 As String, ByVal Hex2 As String) As String
        Dim b1 As Long, b2 As Long, r As Long, s As String
        b1 = HexStr2Dec(Hex1)
        b2 = HexStr2Dec(Hex2)
        r = b1 And b2
        s = Dec2HexStr(r)
        Return s
    End Function
    Public Function BinStrOr(ByVal Bin1 As String, ByVal Bin2 As String) As String
        Dim b1 As Long, b2 As Long, r As Long, s As String
        b1 = BinStr2Dec(Bin1)
        b2 = BinStr2Dec(Bin2)
        r = b1 Or b2
        s = Dec2BinStr(r)
        Return s
    End Function
    Public Function HexStrOr(ByVal Hex1 As String, ByVal Hex2 As String) As String
        Dim b1 As Long, b2 As Long, r As Long, s As String
        b1 = HexStr2Dec(Hex1)
        b2 = HexStr2Dec(Hex2)
        r = b1 Or b2
        s = Dec2HexStr(r)
        Return s
    End Function
    Public Function BinStrXOR(ByVal Bin1 As String, ByVal Bin2 As String) As String
        Dim b1 As Long, b2 As Long, r As Long, s As String
        b1 = BinStr2Dec(Bin1)
        b2 = BinStr2Dec(Bin2)
        r = b1 Xor b2
        s = Dec2BinStr(r)
        Return s
    End Function
    Public Function HexStrXOR(ByVal Hex1 As String, ByVal Hex2 As String) As String
        Dim b1 As Long, b2 As Long, r As Long, s As String
        b1 = HexStr2Dec(Hex1)
        b2 = HexStr2Dec(Hex2)
        r = b1 Xor b2
        s = Dec2HexStr(r)
        Return s
    End Function
    Public Function BinStrNot(ByVal Bin As String) As String
        Dim b As Long, c As Long, s As String
        b = BinStr2Dec(b)
        c = Not b
        s = Dec2BinStr(c)
        Return s
    End Function
    Public Function HexStrNot(ByVal Bin As String) As String
        Dim b As Long, c As Long, s As String
        b = HexStr2Dec(b)
        c = Not b
        s = Dec2HexStr(c)
        Return s
    End Function
    Public Shared Function Dec2BCD(ByVal Dec As Long) As String
        Dim Decs As String, res As String = "", x As String, y As String
        Decs = CStr(Dec)
        For i = 0 To Decs.Length - 1
            x = Dec2BinStr(Decs.Substring(i, 1))
            If x.Length < 4 Then
                Do Until x.Length = 4
                    x = "0" + x
                Loop
            End If
            res = res + x
        Next
        Return res
    End Function
    Public Shared Function BCD2Dec(ByVal BCD As String) As Long
        If BCD.Length / 4 <> Math.Floor(BCD.Length / 4) Then
            Do Until (BCD.Length / 4) = Math.Floor(BCD.Length / 4)
                BCD = "0" + BCD
            Loop
        End If
        Dim res As String = ""
        For i = 1 To BCD.Length / 4
            res = res + CStr(BinStr2Dec(BCD.Substring((i - 1) * 4, 4)))
        Next
        Return CLng(res)
    End Function
End Class
